package com.yeyks.commonReference.append.wxPay.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.yeyks.common.em.pay.PayTypeEnum;
import com.yeyks.common.exception.ServiceException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class WeChatYyksBaseConfigUtil {

    @Autowired
    private WeChatYyksBaseProperties wxProperties;

    //app默认付款对象
    private WxPayService appWxPayService;
    //公众号默认付款对象
    private WxPayService gzhWxPayService;
    //小程序默认付款对象
    private WxPayService xcxWxPayService;

    public WxPayService getWxPayService(String type) {
        if (PayTypeEnum.GZH.desc().equals(type)) {
            return gzhWxPayService;
        } else if (PayTypeEnum.MWEB.desc().equals(type)) {
            return gzhWxPayService;
        } else if (PayTypeEnum.XCX.desc().equals(type)) {
            return xcxWxPayService;
        } else if (PayTypeEnum.WXPAY.desc().equals(type)) {
            return appWxPayService;
        }
        throw new ServiceException("微信付款类型错误");
    }

    @PostConstruct
    public void init() {
        //初始化微信付款对象
        initAppWxPayService();
        initGzhWxPayService();
        initXcxWxPayService();
    }

    private void initAppWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(wxProperties.getAppAppId()));
        payConfig.setMchId(StringUtils.trimToNull(wxProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(wxProperties.getMchKey()));
        payConfig.setKeyPath(StringUtils.trimToNull(wxProperties.getKeyPath()));
        appWxPayService = new WxPayServiceImpl();
        appWxPayService.setConfig(payConfig);
    }

    private void initGzhWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(wxProperties.getGzhAppId()));
        payConfig.setMchId(StringUtils.trimToNull(wxProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(wxProperties.getMchKey()));
        payConfig.setKeyPath(StringUtils.trimToNull(wxProperties.getKeyPath()));
        gzhWxPayService = new WxPayServiceImpl();
        gzhWxPayService.setConfig(payConfig);
    }

    private void initXcxWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(wxProperties.getXcxAppId()));
        payConfig.setMchId(StringUtils.trimToNull(wxProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(wxProperties.getMchKey()));
        payConfig.setKeyPath(StringUtils.trimToNull(wxProperties.getKeyPath()));
        xcxWxPayService = new WxPayServiceImpl();
        xcxWxPayService.setConfig(payConfig);
    }

}
